
<script>
    var citys = @php echo json_encode($citys);@endphp ;
    var provinceCode = '{{$provincecode??""}}';
    var cityCode = '{{$site->city_code??$citycode}}';
    var city = '{{$site->city??""}}';
    var region = '{{$site->region_code??""}}';
    layui.use(['upload', 'layer', 'form', 'laydate'], function () {
        var $ = layui.jquery
            , upload = layui.upload
            , form = layui.form
            , layer = layui.layer
            , laydate = layui.laydate;

        //初始化城市选择
        var html1 = '<option value="">请选择省</option>';
        for (var i in citys) {
            var selected1 = provinceCode == i ? 'selected' : '';
            html1 += '<option value="' + i + '" ' + selected1 + '>' + citys[i].province.name + '</option>';
        }
        if (!!provinceCode) {
            var cityData = citys[provinceCode].city;
            var html2 = '<option value="">请选择市</option>';
            for (var i1 in cityData) {
                var selected2 = cityCode == i1 ? 'selected' : '';
                html2 += '<option value="' + cityData[i1].code + '" ' + selected2 + '>' + cityData[i1].name + '</option>';
            }
        }

        if (!!provinceCode && !!cityCode) {
            var regionData = citys[provinceCode].city[cityCode].region;
            var html3 = '<option value="">请选择县/区</option>';
            for (var i2 in regionData) {
                var selected3 = regionData[i2].code == region ? 'selected' : '';
                html3 += '<option value="' + regionData[i2].code + '" ' + selected3 + '>' + regionData[i2].name + '</option>';
            }
        }

        $('select[name="province_code"]').html(html1);
        $('select[name="city_code"]').html(html2);
        $('input[name="city_code"]').val(city);
        $('select[name="region_code"]').html(html3);
        form.render('select');

        //选择省份
        form.on('select(province)', function (data) {
            var cityInfo = citys[data.value].city;
            var html4 = '<option value="">请选择市</option>';
            for (var i3 in cityInfo) {
                html4 += '<option value="' + cityInfo[i3].code + '">' + cityInfo[i3].name + '</option>';
            }
            $('select[name="city_code"]').html(html4);
            var html5 = '<option value="">请选择县/区</option>'
            $('select[name="region"]').html(html5);
            $("#province_name").val(data.elem[data.elem.selectedIndex].text);
            form.render('select')
        });

        //选择城市
        form.on('select(city)', function (data) {
            var regionInfo = citys[$('select[name="province_code"]').val()].city[data.value].region;
            $('input[name="city_code"]').val(data.elem[data.elem.selectedIndex].text);
            var html6 = '<option value="">请选择县/区</option>';
            for (var i2 in regionInfo) {
                html6 += '<option value="' + regionInfo[i2].code + '" >' + regionInfo[i2].name + '</option>';
            }
            $('select[name="region_code"]').html(html6);
            $("#city_name").val(data.elem[data.elem.selectedIndex].text);
            form.render('select');
        });

        //选择区域
        form.on('select(region)', function (data) {
            //取商圈数据
            var city_code = $("#city_code").val();
            var region_code = $("#region_code").val();
            $("#region_name").val(data.elem[data.elem.selectedIndex].text);
            form.render('select');
        });
        //普通图片上传
        $(".uploadPic").each(function (index, elem) {
            upload.render({
                elem: $(elem)
                , url: '{{ route("api.upload") }}'
                , multiple: false
                , data: {"_token": "{{ csrf_token() }}"}
                , done: function (res) {
                    //如果上传失败
                    if (res.code == 0) {
                        layer.msg(res.msg, {icon: 1}, function () {
                            $(elem).parent('.layui-upload').find('.layui-upload-box').html('<li><img src="' + res.url + '" /><p>上传成功</p></li>');
                            $(elem).parent('.layui-upload').find('.layui-upload-input').val(res.url);
                        })
                    } else {
                        layer.msg(res.msg, {icon: 2})
                    }
                }
            });
        });

        //多图片上传
        $('.myUpload').each(function (idx, elem) {
            upload.render({
                elem: $(elem)
                , url: '{{ route("api.upload") }}' //此处配置你自己的上传接口即可
                , multiple: true
                , data: {"_token": "{{ csrf_token() }}"}
                , done: function (res) {
                    //上传完毕
                    $(elem).parent('.layui-upload').find('.layui-upload-list').append('<div class="layui-upload-img"><img src="' + res.url + '" alt=""><span><i class="layui-icon">&#x1006;</i></span></div>')
                    var arr = [];
                    $(elem).parent('.layui-upload').find('.layui-upload-img').each(function (i, e) {
                        var str = $(e).find('img').attr('src')
                        if (arr.indexOf(str) == -1) {
                            arr.push(str)
                        }
                    })
                    $(elem).parent('.layui-upload').find('.layui-upload-input').val(JSON.stringify(arr))
                }
            });
        });

        form.on('select(device_type)', function (data) {
            var device_type = data.elem[data.elem.selectedIndex].value;
            if (device_type == 2){
                $(".device_gg").show();
                $(".device_lx").hide();
            }else{
                $(".device_gg").hide();
                $(".device_lx").show();
            }
            form.render('select');
        });


        form.on('submit(formDemo)', function (data) {
            $('.layui-form').submit();
        });

    })
</script>
<script src="/baidu-editor/third-party/jquery.min.js"></script>
<script>
    @if (!empty($form['device_type']))
    var device_type = {{$form['device_type']}};
    $("#device_type").val(device_type);
    @endif


</script>
